package com.qf.service.impl;

import com.qf.dao.CartDao;
import com.qf.dao.impl.CartDaoImpl;
import com.qf.domain.Cart;
import com.qf.domain.Goods;
import com.qf.domain.User;
import com.qf.service.CartService;
import com.qf.service.GoodsService;

import java.util.List;

public class CartServiceImpl implements CartService {
    private CartDao cartDao=new CartDaoImpl();
    @Override
    public Cart select(Integer uid, int goods_id) {
        return cartDao.find(uid,goods_id);
    }

    @Override
    public void insert(Cart cart) {
        cartDao.add(cart);
    }

    @Override
    public void update(Cart cart) {
        cartDao.modify(cart);
    }

    @Override
    public List<Cart> selectByUserId(Integer uid) {
        List<Cart> cartList = cartDao.findByUserId(uid);
        if(cartList!=null&&cartList.size()>0){
            GoodsService goodsService=new GoodsServiceImpl();
            for (Cart cart : cartList) {
                Goods goods = goodsService.selectById(cart.getPid());
                cart.setGoods(goods);
            }
        }
        return cartList;
    }

    @Override
    public void delete(Integer uid, int goods_id) {
        cartDao.remove(uid,goods_id);
    }

    @Override
    public void clearAll(Integer uid) {
        cartDao.deleteAll(uid);
    }

    @Override
    public void deleteByUId(int uid) {
        cartDao.removeByUId(uid);
    }
}
